package com.xiaobingby.easyuisys.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xiaobingby.common.entity.TreeNode;
import com.xiaobingby.easyuisys.entity.PermTreegrid;
import com.xiaobingby.easyuisys.entity.Tree;
import com.xiaobingby.easyuisys.service.IPermissionService;
import com.xiaobingby.sys.entity.Menu;
import com.xiaobingby.sys.entity.Permission;
import com.xiaobingby.sys.mapper.PermissionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李磊
 * @date 2019/5/5
 */
@Service("easyuiPermissionService")
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper,Permission> implements IPermissionService {
    @Autowired
    com.xiaobingby.sys.service.IPermissionService permissionService;

    @Override
    public List<Tree> getUserMenu(Long uid){
        List<Menu> menus = permissionService.createMenu(uid);
        List<Tree> trees = new ArrayList<>();
        getTrees(menus,trees);
        return trees;
    }

    @Override
    public List<PermTreegrid> getPermTreegridList() {
        List<Permission> permissions = selectList(null);
        return getTrees(permissions);
    }

    public List<PermTreegrid> getTrees(List<Permission> list){
        Map<Long,PermTreegrid> treeMap = new HashMap<Long,PermTreegrid>();
        for (Permission t : list) {
            PermTreegrid permTreegrid = new PermTreegrid();
            //permTreegrid.setState("closed");
            BeanUtils.copyProperties(t,permTreegrid);
            treeMap.put(permTreegrid.getId(), permTreegrid);
        }
        List<PermTreegrid> retList = new ArrayList<PermTreegrid>();
        for(Map.Entry<Long, PermTreegrid> entry:treeMap.entrySet()){
            Long pid = entry.getValue().getPid();
            PermTreegrid pPermTreegrid = treeMap.get(pid);
            if(pPermTreegrid != null){
                entry.getValue().setParentTreegrid(pPermTreegrid);
                pPermTreegrid.addChild(entry.getValue());
            }else{
                retList.add(entry.getValue());
            }
        }
        //将parentNode置null，否则gson无法转换，fastjson可以
        setParentNodeNull(retList);
        return retList;
    }

    private void setParentNodeNull(List<PermTreegrid> list){
        for (PermTreegrid treeNode : list) {
            treeNode.setParentTreegrid(null);
            setParentNodeNull(treeNode.getChildren());
        }
    }

    private void getTrees(List<Menu> menus,List<Tree> trees){
        for (Menu menu : menus) {
            Tree temp = new Tree();
            temp.setId(menu.getId());
            temp.setChecked(false);
            temp.setIconCls(menu.getIcon());
            temp.setParentId(menu.getPid());
            temp.setState("open");
            temp.setText(menu.getName());
            Map<String,Object> attributes = new HashMap<>();
            String url = menu.getUrl();
            if(url != null && url.startsWith("/admin")){
                url = "/easyui"+url.substring(6);
            }
            attributes.put("url",url);
            temp.setAttributes(attributes);
            if(menu.getChildren()!=null && !menu.getChildren().isEmpty()){
                List<Tree> treeChildren = new ArrayList<>();
                getTrees(menu.getChildren(), treeChildren);
                temp.setChildren(treeChildren);
            }
            trees.add(temp);
        }
    }

}
